<?php
/**
 * Created by PhpStorm.
 * User: hongtianjin
 * Date: 2017/3/1 0001
 * Time: 17:24
 */

namespace App\Models\ActivityPoints;

use App\Contracts\Repositories\ActivityPointsRepository as ActivityPointsRep;
use App\Models\ActivityDelivery\ActivityDeliveryRepository;
use App\Models\ActivityDelivery\ActivityDeliveryService;

class ActivityPointsService
{
    private $activityPointsRepository;

    public function __construct(ActivityPointsRep $activityPointsRepository)
    {
        $this->activityPointsRepository = $activityPointsRepository;
    }

    public function getPointsList()
    {
        return $this->activityPointsRepository->findAll();
    }
    public function createPoints($data)
    {
        $activity_point = new \App\Models\ActivityPoints\ActivityPoints($data);
        return $this->activityPointsRepository->create($activity_point);
    }
    /**
     * @param array $conditions
     * @param bool $softDeletion
     * @return mixed
     */
    public function delete($conditions, $softDeletion = false)
    {
        return $this->activityPointsRepository->delete($conditions, $softDeletion);
    }
    public function deleteActivityPoints($ids)
    {
        //可用的埋点
        $activityDeliveryService = new ActivityDeliveryService(new ActivityDeliveryRepository());
        $availablePoints = $activityDeliveryService->getAvailablePoints();
        $pointIds=[];
        foreach ($availablePoints as $key=>$val) {
            array_push($pointIds, $val['id']);
        }
        $flag = false;
        if (is_array($ids)) {
            foreach ($ids as $val) {
                if (!in_array($val, $pointIds)) {
                    $flag = true;
                    break;
                }
            }
        } elseif (!in_array($ids, $pointIds)) {
            $flag = true;
        }

        return $flag ? false : $this->activityPointsRepository->deleteByIds($ids);
    }
    public function modify($activityPoints, $conditions)
    {
        return $this->activityPointsRepository->modify($activityPoints, $conditions);
    }
    public function findById($id)
    {
        return $this->activityPointsRepository->findById($id);
    }

    public function getPointsListByPage($request, $user_id='', $action='')
    {
        $start = (int)$request->query->get('offset', 0);
        $limit = (int)$request->query->get('limit', 10);
        $keyword = $request->query->get('search');
        $sort = $request->query->get('sort', 'created_at');
        $order = $request->query->get('order', 'desc');
        $sortArray = [[$sort,$order]];

        $conditions = [
            ['user_id', $user_id],
            ['group_code', app('request')->session()->get('groupcode')],
        ];
        if (!is_null($keyword)) {
            $conditions[] = ['name','like','%'.$keyword.'%'];
        }
        //过滤正在使用的埋点
        $pointIds = [];
        if ($action == 'new_point') {
            $activityDeliveryService = new ActivityDeliveryService(new ActivityDeliveryRepository());
            $availablePoints = $activityDeliveryService->getAvailablePoints();
            foreach ($availablePoints as $key=>$val) {
                array_push($pointIds, $val['id']);
            }
        }
        $list = $this->activityPointsRepository->findByConditionsPage($conditions, $sortArray, $start, $limit, $pointIds, $action);

        $list = $list->toArray();

        $total = $this->activityPointsRepository->countByConditions($conditions, $pointIds, $action);

        return ['total'=>$total,'rows'=>$list ];
    }

    public function getAll()
    {
        return $this->activityPointsRepository->findAll();
    }
}
